package at.fhj.itm10.ticketmaster.dao.impl;

import java.sql.Connection;

import org.apache.log4j.Logger;

import at.fhj.itm10.ticketmaster.dao.AddressDAO;
import at.fhj.itm10.ticketmaster.dao.CustomerDAO;
import at.fhj.itm10.ticketmaster.dao.DAOException;
import at.fhj.itm10.ticketmaster.dao.DAOFactory;
import at.fhj.itm10.ticketmaster.dao.EventDAO;
import at.fhj.itm10.ticketmaster.dao.EventTypeDAO;
import at.fhj.itm10.ticketmaster.dao.OrderDAO;
import at.fhj.itm10.ticketmaster.dao.TicketDAO;
import at.fhj.itm10.ticketmaster.services.datasource.DataSourceLocator;
import at.fhj.itm10.ticketmaster.services.impl.EventServiceImpl;

/**
 * DAO factory implementation.
 * 
 * @author Wolfgang Gaar, Kevin Reinmueller, Benjamin Hirt
 */
public class DAOFactoryImpl implements DAOFactory {

	private Connection connection;
	
	private CustomerDAO customerDAO;
	private AddressDAO addressDAO;
	private EventDAO eventDAO;
	private OrderDAO orderDAO;
	private TicketDAO ticketDAO;
	private EventTypeDAO eventtypeDAO;
	
	private Logger logger = Logger.getLogger(DAOFactoryImpl.class);
	
	public DAOFactoryImpl(Connection connection) throws DAOException {
		this.connection = connection;
	}
	
	public DAOFactoryImpl() throws DAOException {
		try {
			connection = (new DataSourceLocator()).getDataSource().getConnection();
		} catch (Exception e) {
			throw new DAOException(e);
		}
	}

	public CustomerDAO getCustomerDAO() throws DAOException {
		if (customerDAO == null) {
			customerDAO = new CustomerDAO(connection);
		}

		return customerDAO;
	}

	public AddressDAO getAddressDAO() throws DAOException {
		if (addressDAO == null)
			addressDAO = new AddressDAO(connection);

		return addressDAO;
	}
	
	/**
	 * Create concrete OrderDAO
	 */
	public OrderDAO getOrderDAO() throws DAOException {
		orderDAO = new OrderDAO(connection);
		
		return orderDAO;
	}
	
	/**
	 * Create concrete TicketDAO
	 */
	public TicketDAO getTicketDAO() throws DAOException {
		ticketDAO = new TicketDAO(connection);
		
		return ticketDAO;
	}

	public EventTypeDAO getEventTypeDAO() throws DAOException {
		if (eventtypeDAO == null)
			eventtypeDAO = new EventTypeDAO(connection);

		return eventtypeDAO;
	}

	public EventDAO getEventDAO() throws DAOException {
		eventDAO = new EventDAO(connection);

		return eventDAO;
	}
}
